package com.neusoft.ruangong.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.ruangong.domain.Conference;
import com.neusoft.ruangong.domain.Reservation;
import com.neusoft.ruangong.domain.Reservations;
import com.neusoft.ruangong.domain.User;
import com.neusoft.ruangong.other.utils.R;
import com.neusoft.ruangong.service.ReservationService;
import com.neusoft.ruangong.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(tags = {"预约模块"})
@CrossOrigin(origins ="*")
@RestController
@RequestMapping("/reservation")
public class ReservationController {
    @Autowired
    private ReservationService reservationService;
    @Autowired
    UserService userService;
    @ApiOperation("添加预约记录")
    @PostMapping("/addReservation")
    public R addReservation1(
                             @RequestParam("roomName")String roomName,
                             @RequestParam("roomUser")String roomUser,
                             @RequestParam("date")String strDate,
                             @RequestParam("begintime")Time begintime,
                             @RequestParam("endtime") Time endtime,
                             @RequestParam("code") String code,
                             @RequestBody List<String> users
                          ) {
        users.add(roomUser);
        Reservation reservation = new Reservation();
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = dateformat.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if(begintime!=null&&endtime!=null){
            reservation.setBegintime(begintime);
            reservation.setEndtime(endtime);
        }
        reservation.setUser(roomUser);
        reservation.setDate(date);
        reservation.setCode(code);
        reservation.setEmail("930113082@qq.com");
        reservation.setMark("待审核");
        reservation.setMobile("无");
        Boolean rest = reservationService.addConference(roomName, reservation, users);
        return rest?R.ok():R.error();
    }
    @ApiOperation("删除预约记录")
    @DeleteMapping("/removeReservation")
    public R removeReservation(@RequestParam("id") Long id){
        boolean b = reservationService.removeById(id);
        System.out.println(b);
        return b?R.ok():R.error();
    }
    @ApiOperation("已加入会议室-用户自己的参加")
    @GetMapping("/queryUseridConference")
    public R queryUseridConference(@RequestParam("id") Long id){
        List<Conference> conferences = reservationService.queryConference(id);
        return R.ok().data("data",conferences);
    }
    @ApiOperation("查询用户自己预约会议室的记录")
    @GetMapping("/queryUseridReservation")
    public R queryUseridConference1(@RequestParam("userName") String userName,
                                    @RequestParam("page") Long index,
                                    @RequestParam("limit") Long limit){
        List<Reservations> reservations = reservationService.queryReservationByName(userName,index-1,limit);
        Integer total = reservationService.queryReservationCount(userName);
        if(total!=null&&total!=0){
            return R.ok().data("data",reservations).data("total",total);
        }else {
            return R.error();
        }
    }
    @ApiOperation("查询未处理的预约记录")
    @GetMapping("/queryReservation")
    public R queryUntreatedReservation(
            @RequestParam("page") Long index,
            @RequestParam("limit") Long limit){
        List<Reservations> list = reservationService.UntreatedReservation(index-1,limit);
        Integer total = reservationService.queryUntreatedSize();
        if(list.size()==0){
            return R.error().data("not found","not found");
        }else {
            return R.ok().data("list",list).data("total",total);
        }
    }
    @GetMapping("/pageReservation")
    @ApiOperation("分页查询预约审核记录-所有的记录进行分页显示")
    public R pageReservation(@RequestParam("page") Long index,
                             @RequestParam("limit" ) Long limit){

        List<Reservations> list = reservationService.pageReservation(index-1, limit);
        Integer total = reservationService.queryPageSize();
        if(list.size()==0){
            return R.error();
        }else {
            return R.ok().data("data",list).data("total",total);
        }
    }

    @ApiOperation("审核预约记录-点击后就审核通过")
    @PutMapping("/modifyReservation")
    public R modifyReservation(
            @RequestParam("id") Long id){
        Integer result = reservationService.modifyReservation(id);
        if(result!=null){
            return R.ok().data("result",result);
        }else {
            return R.error();
        }
    }

    @ApiOperation("审核预约记录-点击后就拒绝通过")
    @PutMapping("/refuseReservation")
    public R modifyReservation1(
            @RequestParam("id") Long id){
        Integer result = reservationService.modifyReservation1(id);
        if(result!=null){
            return R.ok().data("result",result);
        }else {
            return R.error();
        }
    }

    @ApiOperation("搜索预约记录-模糊查询")
    @GetMapping("/searchReservation")
    public R searchAdminReservation(@RequestParam("str") String str){
        List<Reservations> reservations = reservationService.queryLinkReservation(str);
        if(reservations.size()==0){
            return R.error();
        }else {
            return R.ok().data("reservations",reservations);
        }
    }

    @ApiOperation("已加入会议室-搜索功能")
    @GetMapping("/searchUserReservation")
    public R searchUserReservation(@RequestParam("roomName") String roomName,
                                    @RequestParam("userId") Long userId){
        List<Reservations> reservations = reservationService.searchUserReservation(roomName,userId);
        if(reservations.size()==0){
            return R.error();
        }else {
            return R.ok().data("reservations",reservations);
        }
    }

    @ApiOperation("加入预约会议室")
    @PutMapping("/joinReservation")
    public R joinReservation(@RequestParam("userid") BigInteger userId,
                             @RequestParam("reservationid") Long reservationid){
        Integer result = reservationService.addConference(userId,reservationid);
        if(result!=null){
            return R.ok().data("message","添加成功");
        }else {
            return R.error().data("message","添加失败");
        }
    }

    @ApiOperation("判断用户时间是否冲突")
    @PostMapping("/userAllTime")
    public R joinReservation(
                            @RequestParam("date")String strDate,
                            @RequestParam("begintime")Time begintime,
                            @RequestParam("endtime") Time endtime,
                            @RequestBody List<String> users){
        Map result = reservationService.queryAllUserTime(users,strDate,begintime,endtime);
        if(result!=null){
            return R.error().data("message",false).data("data",result);
        }else {
            return R.ok().data("message",true);
        }
    }
}
